package com.example.bingobox.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import com.example.bingobox.R;
import com.example.bingobox.model.State;

public class LocationTypeDialog extends DialogFragment {
    private OnTypeSelectedListener listener;
    private State state;

    public static LocationTypeDialog newInstance(State state) {
        LocationTypeDialog dialog = new LocationTypeDialog();
        dialog.state = state;
        return dialog;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
        LayoutInflater inflater = requireActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_location_type, null);

        RadioGroup typeGroup = view.findViewById(R.id.type_group);
        EditText customType = view.findViewById(R.id.custom_type);

        // 动态添加已有的类型选项
        int id = View.generateViewId();
        for (String type : state.getLocationTypes()) {
            RadioButton button = new RadioButton(requireContext());
            button.setId(id++);
            button.setText(type);
            typeGroup.addView(button);
        }

        // 添加自定义选项
        RadioButton customButton = new RadioButton(requireContext());
        customButton.setId(R.id.type_custom);
        customButton.setText("自定义");
        typeGroup.addView(customButton);

        // 显示/隐藏自定义输入框
        typeGroup.setOnCheckedChangeListener((group, checkedId) -> {
            customType.setVisibility(checkedId == R.id.type_custom ? View.VISIBLE : View.GONE);
        });

        return builder.setView(view)
                .setPositiveButton("确定", (dialog, which) -> {
                    String type;
                    int checkedId = typeGroup.getCheckedRadioButtonId();
                    if (checkedId == R.id.type_custom) {
                        String customText = customType.getText().toString().trim();
                        if (!customText.isEmpty()) {
                            type = customText;
                            // 将新的自定义类型添加到集合中
                            state.addLocationType(type);
                        } else {
                            type = "未标记";
                        }
                    } else {
                        RadioButton selectedButton = view.findViewById(checkedId);
                        type = selectedButton.getText().toString();
                    }
                    if (listener != null) {
                        listener.onTypeSelected(type);
                    }
                })
                .setNegativeButton("取消", null)
                .create();
    }

    public interface OnTypeSelectedListener {
        void onTypeSelected(String type);
    }

    public void setOnTypeSelectedListener(OnTypeSelectedListener listener) {
        this.listener = listener;
    }
} 